/*****************************
	gulp clean (- 删除)
	gulp build (- 编译) 
	gulp watch (- 实时编译) 
*****************************/
/*****************************

	安装必要插件
	npm install --save-dev gulp gulp-imagemin imagemin-pngquant del gulp-jshint gulp-uglify gulp-minify-css
*****************************/

'use strict';
var gulp = require('gulp'),
	imagemin = require('gulp-imagemin'),
	pngquant = require('imagemin-pngquant'),
	del = require('del'),
	jshint = require('gulp-jshint'),
	uglify = require('gulp-uglify'),
	//concat = require('gulp-concat'),
	//ignore = require('gulp-ignore'),
	//fontSpider = require('gulp-font-spider'),
	minifyCss = require('gulp-minify-css');

/*****************/
//配置

var _destroot = "dest/";
var _devroot = 'dev/';
/*****************/



gulp.task('clean',function(cb) {
	del(_destroot,cb);
});


gulp.task('default',['clean'],function() {
	gulp.start('minifyJs','minifycss','minifyimg');
});

/*gulp.task('cleanMinify',['copy'],function(cb) {
	del([_destroot + 'css',_destroot + 'js/system',_destroot + 'lib/scroll',_destroot + 'images'],cb);
})

gulp.task('build',['cleanMinify'],function() {
	gulp.start('minifyJs','minifycss','minifyimg');
});*/

gulp.task('build',['copy'],function() {
	gulp.start('minifyJs','minifycss','minifyimg');
});

gulp.task('copy',['clean'],function(cb) {
	return gulp.src(_devroot + '**/*')
			.pipe(gulp.dest(_destroot));
});

gulp.task('cleanJs',function() {
	del(_destroot + '**/*.js');
});
gulp.task('cleanCss',function() {
	del(_destroot + '**/*.css');
});
gulp.task('cleanImg',function() {
	del([_destroot + '**/*.png',_destroot + '**/*.jpg',_destroot + '**/*.gif']);
});

// 检查js
gulp.task('jshint', function() {
  return gulp.src([_devroot + '**/*.js','!' + _devroot + '**/*.min.js'])
    .pipe(jshint())
    .pipe(jshint.reporter('default'));
});

//************** 压缩JS **************
gulp.task('minifyJs',function() {
	return gulp.src([_devroot + '**/*.js','!' + _devroot + '**/*.min.js'])
			.pipe(jshint())
			//.pipe(jshint.reporter('YOUR_REPORTER_HERE'))
			//.pipe(ignore.exclude([_devroot + '**/*.map']))
			.pipe(uglify())
			.on('error',function(error) {
				console.log(error);
			})
			//.pipe(concat('main.js'))
			.pipe(gulp.dest(_destroot));
});
//************** 压缩CSS ***************
gulp.task('minifycss',function() {
	return gulp.src([_devroot + '**/*.css'])
			//.pipe(concat('app.min.css'))
			.pipe(minifyCss())
			.pipe(gulp.dest(_destroot));
});
//************ 压缩图片 *************
gulp.task('minifyimg',function() {
	return gulp.src([_devroot + '**/*.png',_devroot + '**/*.jpg',_devroot + '**/*.gif'])
			.pipe(imagemin({
				optimizationLevel: 5,
				progressive: true,
				interlaced: true,
				multipass: true,
				use: [pngquant()]
			}))
			.pipe(gulp.dest(_destroot));
});
//************ 压缩字体 *************
//gulp.task('fontspider',function() {
//	return gulp.src(_destroot + '**/*.html')
//			.pipe(fontSpider());
//});
//************ 监视 *************
gulp.task('watch',function() {
	gulp.watch(_devroot + '**/*.js',['cleanJs','minifyJs']);
	gulp.watch(_devroot + '**/*.css',['cleanCss','minifycss']);
	gulp.watch([_devroot + '**/*.png',_devroot + '**/*.jpg',_devroot + '**/*.gif'],['cleanImg','minifyimg']);
})